﻿using Coldairarrow.Entity.MEGA;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.MEGA
{
    public class MEGA_LeafBladeAndMaterialBusiness : BaseBusiness<MEGA_LeafBladeAndMaterial>, IMEGA_LeafBladeAndMaterialBusiness, ITransientDependency
    {
        public MEGA_LeafBladeAndMaterialBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<MEGA_LeafBladeAndMaterialDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_LeafBladeAndMaterial>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<MEGA_LeafBladeAndMaterial, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
            q = q.Where(where);
            var qq = from a in q
                     join b in Db.GetIQueryable<MEGA_Material>() on a.MId equals b.Id
                     select new MEGA_LeafBladeAndMaterialDTO
                     {
                         Batch = b.Batch,
                         Code = b.Code,
                         Num = a.Num.ToString(),
                         CreateTime = a.CreateTime,
                         CreatorId = a.CreatorId,
                         Id = a.Id,
                         Image = b.Image,
                         JGTime = b.JGTime,
                         LBId = a.LBId,
                         MId = a.MId,
                         Name = b.Name,
                         Note = b.Note,
                         Parts = b.Parts,
                         TypeId = b.TypeId,
                         TypeName = b.TypeName
                     };
            return await qq.GetPageResultAsync(input);
        }

        public async Task<MEGA_LeafBladeAndMaterial> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(MEGA_LeafBladeAndMaterial data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_LeafBladeAndMaterial data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}